/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getUserInfo();
    var formData = {
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'item/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拼接字符串
function getHtml(d){
    var h = '', i = 0;
    for (; i < d.length; i ++) {
        h += '<table class="tbox" cellspacing="0">';
        h += '<tr>';
        h += '<td class="tdl">项目名称</td>';
        h += '<td class="tdr">' + d[i].title + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">开始时间</td>';
        h += '<td class="tdr">' + d[i].starttime + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">结束时间</td>';
        h += '<td class="tdr">' + d[i].endtime + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">负责人</td>';
        h += '<td class="tdr">' + d[i].leader_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl"></td>';
        h += '<td class="tdr">';
        h += '<a href="javascript:;" onclick="getOne(' + d[i].id + ');">修改</a>';
        // h += '<a href="javascript:;" onclick="edit(' + d[i].id + ');">修改</a>';
        // h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#tbody').html(h);
}


// 获取详情
function getOne(v){
    var formData = {
        id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'item/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            editHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//编辑弹窗
function editHtml(e){
    var d = e.data, leader = e.leader;;
    var h = '', i;
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt" style="height: 60vh; top: 10vh;">';
    h += '<div>项目名称</div>';
    h += '<input type="text" id="title" placeholder="输入名称" value="' + d.title + '"/>';
    h += '<div>负责人</div>';
    h += '<select id="leader_id">';
    if (d.leader_id == 0) {
        h += '<option value="0" selected>-选择-</option>';
    } else {
        h += '<option value="0">-选择-</option>';
    }
    for (i = 0; i < leader.length; i ++) {
        if (d.leader_id == leader[i].id) {
            h += '<option value="' + leader[i].id + '" selected>';
        } else {
            h += '<option value="' + leader[i].id + '">';
        }
        h += leader[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div>开始日期</div>';
    h += '<input type="text" placeholder="点击选择" onfocus="WdatePicker();" id="startday" class="Wdate" value="' + d.starttime + '" readonly/>';
    h += '<div>结束日期</div>';
    h += '<input type="text" placeholder="点击选择" onfocus="WdatePicker();" id="endday" class="Wdate" value="' + d.endtime + '" readonly/>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="modify(' + d.id + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


////编辑弹窗
//function edit(v){
//    var title = $('#title' + v).val();
//    var h = '';
//    h += '<div class="popMask"></div>';
//    h += '<div class="popCnt">';
//    h += '<div>部门名称</div>';
//    h += '<input type="text" id="title" value="' + title + '"/>';
//    h += '<div class="popBtn">';
//    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
//    h += '<button onclick="modify(' + v + ');">保存</button>';
//    h += '</div>';
//    h += '</div>';
//    $('#formPop').html(h);
//}


//编辑保存
function modify(v){
    var uinfo = getUserInfo();
   var title = $('#title').val();
   var leader_id = $('#leader_id').val();
   var startday = $('#startday').val();
   var endday = $('#endday').val();
   var formData = {
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
       id: v, title: title, leader_id: leader_id,
       startday: startday, endday: endday,
   };
   //console.log(formData); return
   $.ajax({
       type: apiMethod,
       url: apiDomain + 'item/modify',
       data: formData,
       success: function (res) {
           console.log(res);
           if (res.code < 0) {
               msgPop({msg: res.msg});
               return
           }
           msgPop({msg: '成功'});
           setTimeout(function(){
               window.location.reload();
           }, 500);
       },
       error: function () {
           console.log('网络错误');
       }
   });
}